Automation Test Practice

Follow me on GitHub

Dynamic Mock Building Using Groovy on SoapUI

Nowadays, more and more Web Service are developed. Very often a web service cannot function independently, it relies on input from one or more other service. When QA test a specific Web Service, other service may not be available yet, or it cannot cooperate to input some specific value to allow QA cover enough negative scenario. An efficient solution is to build mocks to simulate those external services with various specific response to mitigate the dependence.

When we conduct Automation Test, we always need Data Driven approach. Thus it’s not enough if a mock can only returns seveal specific static response. We expect the mock can response dynamical per the test data to cover an expected scenario.

In this section, I’d like to introduce how to use Groovy on SoapUI to build dynamic mocks to facilitate Automation Test.

Groovy is a Java-like syntax language, but with much easier and short learning curve like Python.

This is not a step-by-step tutorial, but I will list couple of key points which may be challeangable for beginners.

How to parse the request

To make the mock response dynamically, we firstly need to parse the request sent to the mock. Nowadays request sent to a web service can be with various format, Json, XML, encrypted string or even simple text. SoapUI has different approach to parse these request.

XML

Take following XML request as example:
xml_1.png

To get the Street element value, we can write groovy script like this:

def holder = new com.eviware.soapui.support.XmlHolder( mockRequest.requestContent )
def street = holder[//Street”]

SOAP

SOAP request is actually another xml format like following:

xml_2.png

we can get a specific element value like this.

def slurper = new XmlSlurper().parseText(mockRequest.requestContent )
def lastName = slurper.Body.InitialRequest.Identity.Name.LastName

JSON

Parsing json is probably the most often used as most Web Service are communicated in json now.

xml_3.png

def slurp = new groovy.json.JsonSlurper().parseText( mockRequest.requestContent )
def lastname = slurp.inputData.lastName

Encripted Request

Some request is encripted for security concern, depends on the encription method, the mock needs to decode the request to plain text at first. Following is an example:

decodeMsg = java.net.URLDecoder.decode(mockRequest.getRequestContent(), UTF-8);

Simple text is simple. We can retrieve the string directly then split it.

def queryString = mockRequest.getRequest().getQueryString()

Response Dynamically

To response dynamically, we need to be able to

  • select response based on request
  • put variable in response String

soap_1.png

And in the response, we use the variable as following:

soap_2.png

When the Mock is finished, it is easy to use SoapUI save the project as a .war file thus you can deploy it onto an application server. It can work as an independent service to interact with your AUT(Application Under Test).

Back To Homepage